package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.AjaxResponse;
import com.zretc.emptraining.service.EmpService;
import com.zretc.emptraining.service.UsersService;
import com.zretc.emptraining.serviceimpl.EmpServiceImpl;
import com.zretc.emptraining.serviceimpl.UsersServiceImp;

/**
 * Servlet implementation class MessageLoginServlet
 */
@WebServlet("/MessageLoginServlet")
public class MessageLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	UsersService us = new UsersServiceImp();
	EmpService es = new EmpServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String op = "";
		if(request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		String choose = "";
		if(request.getParameter("choose") != null) {
			choose = request.getParameter("choose");
		}
        String mobile = request.getParameter("mobile");
        String code = request.getParameter("code");
        
        System.out.println(mobile);
        System.out.println(code);
        
        //从session中拿出数据
        HttpSession session = request.getSession();
        String code_session = (String)session.getAttribute("code");
        String number = (String)session.getAttribute("mobile");
        //清除session中的数据
        session.removeAttribute("code");
        session.removeAttribute("number");
        
        System.out.println(code_session);
        System.out.println(number);

        if(code_session == null || code_session.trim().equals("")) {
            System.out.println("验证码为空！");
        }
        //发送验证码的手机号码和登录时得到手机号码必须一致
        if(!number.trim().equalsIgnoreCase(mobile)) {
            System.out.println("手机号码不一致！");
        }
        
        String msg = "0";
        if(op.equals("updPwd")) {
        	System.out.println("进入密码修改页面");
        	msg = "100";
        	Gson(msg, response);
        }else {
	        if(code_session.trim().equalsIgnoreCase(code)) {
	        	doLogin(request,response,mobile,choose);
	            System.out.println("登录成功！");
	        } else {
	            System.out.println("验证码不一致!");
	            Gson(msg,response);
	        }
        }
	}
	
	private void doLogin(HttpServletRequest request, HttpServletResponse response ,String mobile , String choose)
			throws ServletException, IOException {
		// 用户身份
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		String msg = "登陆失败";
		int code = 0;
		if ("emp".equals(choose)) {
			System.out.println(choose);
			// 员工
			// 通过手机号获得员工编号
			int empNo = us.getUsersEmpNo(mobile).getEmpNo();
			// 通过员工编号获得员工职位
			String job = es.getEmpsByNo(empNo).getEmpJob();
			if ("主管".equals(job)) {
				msg = "主管，登录成功！";
				code = 1;
			} else if ("教员".equals(job)) {
				msg = "教员，登录成功！";
				code = 2;
			} else {
				msg = "员工，登录成功！";
				code = 3;
			}
		}
		if ("admin".equals(choose)) {
			msg = "管理员，登录成功！";
			code = 4;
		}
		// 账号密码错误
		AjaxResponse ar = new AjaxResponse(msg, code);
		System.out.println(ar);
		// 将ar转换为JSON格式的字符串
		Gson gson = new Gson();
		String msgjson = gson.toJson(ar);
		System.out.println(msgjson);
		// 响应
		PrintWriter out = response.getWriter();
		out.print(msgjson);
		// 关闭
		out.close();
	}
	
	public void Gson(String keyword, HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		String toJson = gson.toJson(keyword);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(toJson);
		printWriter.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
